Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

pkg/tinydtls: Implement sock_aux_local #14706

Merged
merged 1 commit into from
Dec 4, 2020
Merged

Conversation

maribu
Copy link
Member

@maribu maribu commented Aug 5, 2020

Contribution description

This PR adds the implementation of the sock_aux_local for tinydtls, that provides access to the local address via the extended SOCK API of #14703.

Testing procedure

No testing application provided :-/

Issues/PRs references

Depends on

@maribu maribu requested review from miri64 and jia200x August 5, 2020 07:48
@miri64 miri64 added Area: network Area: Networking Area: pkg Area: External package ports Area: security Area: Security-related libraries and subsystems State: waiting for other PR State: The PR requires another PR to be merged first Type: new feature The issue requests / The PR implemements a new feature for RIOT labels Aug 6, 2020
@maribu
Copy link
Member Author

maribu commented Dec 3, 2020

Rebased on top of #14704

@miri64
Copy link
Member

miri64 commented Dec 3, 2020

Depends on #14703 and #14704

Isn't the lwIP PR a dependency as well?

@maribu maribu changed the title pkg/tinydtls: Implement sock_aux_local pkg/tinydtls: Implement sock_aux_local Dec 3, 2020
@maribu
Copy link
Member Author

maribu commented Dec 3, 2020

Depends on #14703 and #14704

Isn't the lwIP PR a dependency as well?

Yes, indeed. I updated the PR description.

@miri64 miri64 removed the State: waiting for other PR State: The PR requires another PR to be merged first label Dec 4, 2020
@miri64
Copy link
Member

miri64 commented Dec 4, 2020

All dependencies are merged. Please rebase to current master.

@miri64
Copy link
Member

miri64 commented Dec 4, 2020

No testing application provided :-/

While the code is simple enough, do you have a suggestion how to test this?

@miri64
Copy link
Member

miri64 commented Dec 4, 2020

I applied the following patch

diff --git a/examples/dtls-sock/Makefile b/examples/dtls-sock/Makefile
index 8861948702..1a15299175 100644
--- a/examples/dtls-sock/Makefile
+++ b/examples/dtls-sock/Makefile
@@ -23,6 +23,8 @@ USEPKG += tinydtls
 # Pull in sock APIs
 USEMODULE += sock_dtls
 USEMODULE += sock_udp
+USEMODULE += sock_aux_local
+USEMODULE += sock_util
 
 # tinydtls needs crypto secure PRNG
 USEMODULE += prng_sha1prng
diff --git a/examples/dtls-sock/dtls-client.c b/examples/dtls-sock/dtls-client.c
index c5cb43a894..093cf24f9d 100644
--- a/examples/dtls-sock/dtls-client.c
+++ b/examples/dtls-sock/dtls-client.c
@@ -20,6 +20,7 @@
 
 #include "net/sock/udp.h"
 #include "net/sock/dtls.h"
+#include "net/sock/util.h"
 #include "net/ipv6/addr.h"
 #include "net/credman.h"
 
@@ -76,6 +77,7 @@ static int client_send(char *addr_str, char *data, size_t datalen)
     sock_dtls_session_t session;
     sock_udp_ep_t remote = SOCK_IPV6_EP_ANY;
     sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
+    sock_dtls_aux_rx_t aux = { .flags = SOCK_AUX_GET_LOCAL };
     local.port = 12345;
     remote.port = DTLS_DEFAULT_PORT;
     uint8_t buf[DTLS_HANDSHAKE_BUFSIZE];
@@ -128,8 +130,8 @@ static int client_send(char *addr_str, char *data, size_t datalen)
         return res;
     }
 
-    res = sock_dtls_recv(&dtls_sock, &session, buf, sizeof(buf),
-                         SOCK_NO_TIMEOUT);
+    res = sock_dtls_recv_aux(&dtls_sock, &session, buf, sizeof(buf),
+                             SOCK_NO_TIMEOUT, &aux);
     if (res != -SOCK_DTLS_HANDSHAKE) {
         printf("Error creating session: %d\n", (int)res);
         sock_dtls_close(&dtls_sock);
@@ -137,6 +139,13 @@ static int client_send(char *addr_str, char *data, size_t datalen)
         return -1;
     }
     printf("Connection to server successful\n");
+    if (!(aux.flags & SOCK_AUX_GET_LOCAL)) {
+        uint16_t port;
+        char addr[IPV6_ADDR_MAX_STR_LEN];
+
+        sock_udp_ep_fmt(&aux.local, addr, &port);
+        printf("on local end-point [%s]:%u\n", addr, port);
+    }
 
     if (sock_dtls_send(&dtls_sock, &session, data, datalen, 0) < 0) {
         puts("Error sending data");
@@ -145,10 +154,18 @@ static int client_send(char *addr_str, char *data, size_t datalen)
         printf("Sent DTLS message\n");
 
         uint8_t rcv[512];
-        if ((res = sock_dtls_recv(&dtls_sock, &session, rcv, sizeof(rcv),
-                                    SOCK_NO_TIMEOUT)) >= 0) {
+        aux.flags |= SOCK_AUX_GET_LOCAL;
+        if ((res = sock_dtls_recv_aux(&dtls_sock, &session, rcv, sizeof(rcv),
+                                      SOCK_NO_TIMEOUT, &aux)) >= 0) {
             printf("Received %d bytes: \"%.*s\"\n", (int)res, (int)res,
                    (char *)rcv);
+            if (!(aux.flags & SOCK_AUX_GET_LOCAL)) {
+                uint16_t port;
+                char addr[IPV6_ADDR_MAX_STR_LEN];
+
+                sock_udp_ep_fmt(&aux.local, addr, &port);
+                printf("on local end-point [%s]:%u\n", addr, port);
+            }
         }
     }
 

And I get the expected output:

dtlsc fe80::204:2519:1801:bcd8 abcd 
2020-12-04 16:31:56,976 # dtlsc fe80::204:2519:1801:bcd8 abcd
2020-12-04 16:31:57,071 # Connection to server successful
2020-12-04 16:31:57,075 # on local end-point [fe80::204:2519:1801:ab27]:12345
2020-12-04 16:31:57,078 # Sent DTLS message
2020-12-04 16:31:57,093 # Received 4 bytes: "abcd"
2020-12-04 16:31:57,099 # on local end-point [fe80::204:2519:1801:ab27]:12345
2020-12-04 16:31:57,100 # Terminating
> ifconfig
2020-12-04 16:32:55,292 #  ifconfig
2020-12-04 16:32:55,298 # Iface  5  HWaddr: 2B:27  Channel: 26  Page: 0  NID: 0x23  PHY: O-QPSK 
2020-12-04 16:32:55,299 #           
2020-12-04 16:32:55,303 #           Long HWaddr: 00:04:25:19:18:01:AB:27 
2020-12-04 16:32:55,310 #            TX-Power: 0dBm  State: IDLE  max. Retrans.: 3  CSMA Retries: 4 
2020-12-04 16:32:55,318 #           AUTOACK  ACK_REQ  CSMA  L2-PDU:102  MTU:1280  HL:64  6LO  
2020-12-04 16:32:55,319 #           IPHC  
2020-12-04 16:32:55,322 #           Source address length: 8
2020-12-04 16:32:55,325 #           Link type: wireless
2020-12-04 16:32:55,331 #           inet6 addr: fe80::204:2519:1801:ab27  scope: link  VAL
2020-12-04 16:32:55,334 #           inet6 group: ff02::1
2020-12-04 16:32:55,335 #    

Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@miri64 miri64 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Dec 4, 2020
@miri64 miri64 merged commit 5673ada into RIOT-OS:master Dec 4, 2020
@maribu
Copy link
Member Author

maribu commented Dec 4, 2020

Thanks for reviewing and especially for taking the time to adapt the example application to properly test this!

@maribu maribu deleted the sock-aux-dtls branch December 4, 2020 16:23
@miri64
Copy link
Member

miri64 commented Dec 4, 2020

[…] and especially for taking the time to adapt the example application to properly test this!

With pleasure :-)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Area: network Area: Networking Area: pkg Area: External package ports Area: security Area: Security-related libraries and subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants